// 安全的存储工具类
export default class Storage {
  static isAvailable() {
    return typeof wx !== 'undefined' && wx.setStorageSync && wx.getStorageSync;
  }

  static set(key, value) {
    try {
      if (this.isAvailable()) {
        wx.setStorageSync(key, value);
        return true;
      } else {
        console.warn('存储API不可用');
        return false;
      }
    } catch (error) {
      console.warn('存储操作失败:', error);
      return false;
    }
  }

  static get(key, defaultValue = null) {
    try {
      if (this.isAvailable()) {
        return wx.getStorageSync(key) || defaultValue;
      } else {
        console.warn('存储API不可用');
        return defaultValue;
      }
    } catch (error) {
      console.warn('读取存储失败:', error);
      return defaultValue;
    }
  }

  static remove(key) {
    try {
      if (this.isAvailable()) {
        wx.removeStorageSync(key);
        return true;
      } else {
        console.warn('存储API不可用');
        return false;
      }
    } catch (error) {
      console.warn('删除存储失败:', error);
      return false;
    }
  }

  static clear() {
    try {
      if (this.isAvailable()) {
        wx.clearStorageSync();
        return true;
      } else {
        console.warn('存储API不可用');
        return false;
      }
    } catch (error) {
      console.warn('清空存储失败:', error);
      return false;
    }
  }

  static getInfo() {
    try {
      if (this.isAvailable()) {
        return wx.getStorageInfoSync();
      } else {
        console.warn('存储API不可用');
        return null;
      }
    } catch (error) {
      console.warn('获取存储信息失败:', error);
      return null;
    }
  }
} 